package com.sinothk.hussars.controller;

import com.sinothk.hussars.entity.LoginVo;
import com.sinothk.hussars.entity.LoginWorkerDTO;
import com.sinothk.hussars.service.LoginService;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "登录相关功能")
@RestController
@RequestMapping("/auth")
@ResponseResult()
public class LoginController {

    private final LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    @ApiOperation(value = "登录-工作人员")
    @ApiImplicitParams({@ApiImplicitParam(name = "orgType", value = "机构类型:01监督机构 02检测机构 03采集机构", required = true),@ApiImplicitParam(name = "account", value = "账号", required = true), @ApiImplicitParam(name = "password", value = "密码", required = true)})
    @PostMapping("/loginForWorker")
    public LoginWorkerDTO loginForWorker(@RequestParam(value = "orgType") String orgType, @RequestParam(value = "account") String account, @RequestParam(value = "password") String password) {
        // 参数验证
        if (StringUtil.isEmpty(orgType)) throw new BizException("机构类型不能为空");
        if (StringUtil.isEmpty(account)) throw new BizException("账号不能为空");
        if (StringUtil.isEmpty(password)) throw new BizException("密码不能为空");

        return loginService.loginForWorker(orgType, account, password);
    }

    @ApiOperation(value = "登录-受检人员")
    @ApiImplicitParams({@ApiImplicitParam(name = "idNum", value = "身份证号码", required = true), @ApiImplicitParam(name = "password", value = "密码", required = true)})
    @PostMapping("/loginForNormal")
    public LoginVo login(@RequestParam(value = "idNum") String idNum, @RequestParam(value = "password") String password) {
        // 参数验证
        if (StringUtil.isEmpty(idNum)) throw new BizException("身份证号码不能为空");
        if (StringUtil.isEmpty(password)) throw new BizException("密码不能为空");

        return loginService.login(idNum, password);
    }
}
